package caj.springboot.handle;

import caj.springboot.common.Result;
import caj.springboot.common.ResultCode;
import caj.springboot.model.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;


/**
 * @description: 全局统一异常处理
 * @author: CAJ
 * @time 2023/10/19 15:31
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 拦截自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public Result handleBaseException(BaseException e) {
        log.error("自定义BaseException异常信息：{}", e.getMessage());
        return Result.error(e.getResultCodeEnum().getCode(),e.getMessage());
    }

    // 参数校验异常
    @ExceptionHandler(ConstraintViolationException.class)
    public Result handleConstraintViolationException(ConstraintViolationException e) {
        String message = e.getConstraintViolations().stream()
                .map(ConstraintViolation::getMessage)
                        .collect(Collectors.joining(";"));
        log.error("ConstraintViolationException异常信息：{}", message);
        return Result.error(ResultCode.PARAM_ERROR.getCode(),message);
    }

    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        String message = e.getBindingResult().getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining(";"));
        log.error("Bind异常信息：{}", message);
        return Result.error(ResultCode.PARAM_ERROR.getCode(),message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining(";"));
        log.error("MethodArgumentNotValidException异常信息：{}", message);
        return Result.error(ResultCode.PARAM_ERROR.getCode(),message);
    }

    // 算数异常
    @ExceptionHandler(ArithmeticException.class)
    public Result handleArithmeticException(ArithmeticException e) {
        log.error("算术异常信息：{}", e.getMessage());
        return Result.error(ResultCode.ARITHMETIC_ERROR.getCode(),e.getMessage());
    }

    // 运行时异常
    @ExceptionHandler(RuntimeException.class)
    public Result handleRuntimeException(RuntimeException e) {
        log.error("运行时异常信息：{}", e.getMessage());
        return Result.error(ResultCode.RUNTIME_ERROR.getCode(),e.getMessage());
    }

    // 异常
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("未知异常信息：{}", e.getMessage());
        return Result.error(ResultCode.SYS_ERROR.getCode(),e.getMessage());
    }

}

